var express = require('express');
var router = express.Router();
var query = require("../db/db").query;
/* 1. 引入上传图片模块 */
const multer = require("multer");
const upload = multer({ dest: __dirname + "/../uploadimg" });

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
// 上传图片接口
router.post('/uploadImg', upload.single("file"),function(req, res, next) {
  const file = req.file;
  file.url = `${file.filename}`;
  res.send(file);
});
router.post('/login', function(req, res, next) {
  let {username,password}= req.body;
  let sql = "SELECT * from myshop.user where username=? and password =?";
  let values = [username,password];
  query(sql,values,function(err,results){
    if(results.length === 0){
      res.json({
        code:-1,
        msg:"账号密码不正确",
        data:[]
      })
    }else{
      // 获取菜单数据 返回前台
      req.session.user_id = results[0].user_id;
      res.json({
        code:1,
        msg:"登录成功",
        data:[]
      })
    }
  })
});

module.exports = router;
